package cn.com.nes.site.service.product.handle;

import cn.com.nes.mybatis.agent.ope.entity.ProductSeriesAndEnergy;
import cn.com.nes.mybatis.agent.ope.mapper.ProductSeriesAndEnergyMapper;
import cn.com.nes.site.entity.vo.product.SaveProductSeriesAndEnergyVO;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 产品属性处理器
 */
public abstract class ProductAttributeHandler {

    @Resource
    ProductSeriesAndEnergyMapper productSeriesAndEnergyMapper;

    /**
     * 返回产品属性
     */
    public abstract Integer getNodeType();

    /**
     * 新增节点
     */
    public abstract Long addNode(SaveProductSeriesAndEnergyVO vo);

    /**
     * 更新节点
     */
    public abstract Long updateNode(SaveProductSeriesAndEnergyVO vo);

    // 获取产品属性的实例
    protected ProductSeriesAndEnergy getProductAttributeEntity(SaveProductSeriesAndEnergyVO vo) {
        ProductSeriesAndEnergy productSeriesAndEnergy = new ProductSeriesAndEnergy();
        BeanUtils.copyProperties(vo, productSeriesAndEnergy);
        productSeriesAndEnergy.setLevel(0); // 0 - 根节点
        if (0L != vo.getParentId()) {
            Integer level = productSeriesAndEnergyMapper.getLevel(vo.getParentId());
            productSeriesAndEnergy.setLevel(level + 1);
        }
        // 设置排序
        Integer sort = productSeriesAndEnergy.getSort();
        if (Objects.isNull(sort)) {
            sort = productSeriesAndEnergyMapper.getChildMaxSort(vo.getParentId());
            productSeriesAndEnergy.setSort(Objects.isNull(sort) ? 1 : sort + 1);
        }
        if(ObjectUtil.isNotNull(vo.getUserInfo())){
            productSeriesAndEnergy.setAreaCode(vo.getUserInfo().getAreacode());
        }
        productSeriesAndEnergy.setDeleted(false);
        productSeriesAndEnergy.setGmtCreate(vo.getOperateDate());
        productSeriesAndEnergy.setGmtCreateId(vo.getOperateUserId());
        productSeriesAndEnergy.setGmtModified(vo.getOperateDate());
        productSeriesAndEnergy.setGmtModifiedId(vo.getOperateUserId());

        return productSeriesAndEnergy;
    }
}
